package com.peas.platform.common.core.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.common.core.exception.BaseException;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

public class AssertUtil {

    public static void isTrue(boolean expression, String code, String message) {
        if (!expression) {
            throw new BaseException(code, message);
        }
    }

    public static void isFalse(boolean expression, String code, String message) {
        if (expression) {
            throw new BaseException(code, message);
        }
    }

    public static void isNull(Object object, String code, String message) {
        isTrue(ObjectUtil.isNull(object), code, message);
    }

    public static void notNull(Object object, String code, String message) {
        isTrue(ObjectUtil.isNotNull(object), code, message);
    }

    public static <T extends CharSequence> void isEmpty(T text, String code, String message) {
        isTrue(StrUtil.isEmpty(text), code, message);
    }

    public static <T extends CharSequence> void notEmpty(T text, String code, String message) {
        isTrue(StrUtil.isNotEmpty(text), code, message);
    }

    public static void isEmpty(Collection<?> collection, String code, String message) {
        isTrue(CollUtil.isEmpty(collection), code, message);
    }

    public static void notEmpty(Collection<?> collection, String code, String message) {
        isTrue(CollUtil.isNotEmpty(collection), code, message);
    }

    public static void isEmpty(Iterable<?> iterable, String code, String message) {
        isTrue(CollUtil.isEmpty(iterable), code, message);
    }

    public static void notEmpty(Iterable<?> iterable, String code, String message) {
        isTrue(CollUtil.isNotEmpty(iterable), code, message);
    }

    public static void isEmpty(Iterator<?> iterator, String code, String message) {
        isTrue(CollUtil.isEmpty(iterator), code, message);
    }

    public static void notEmpty(Iterator<?> iterator, String code, String message) {
        isTrue(CollUtil.isNotEmpty(iterator), code, message);
    }

    public static void isEmpty(Enumeration<?> enumeration, String code, String message) {
        isTrue(CollUtil.isEmpty(enumeration), code, message);
    }

    public static void notEmpty(Enumeration<?> enumeration, String code, String message) {
        isTrue(CollUtil.isNotEmpty(enumeration), code, message);
    }

    public static void isEmpty(Map<?, ?> map, String code, String message) {
        isTrue(CollUtil.isEmpty(map), code, message);
    }

    public static void notEmpty(Map<?, ?> map, String code, String message) {
        isTrue(CollUtil.isNotEmpty(map), code, message);
    }

    public static <T extends CharSequence> void isBlank(T text, String code, String message) {
        isTrue(StrUtil.isBlank(text), code, message);
    }

    public static <T extends CharSequence> void notBlank(T text, String code, String message) {
        isTrue(StrUtil.isNotBlank(text), code, message);
    }

    public static <T extends CharSequence> void isContain(T textToSearch, T substring, String code, String message) {
        isTrue(StrUtil.contains(textToSearch, substring), code, message);
    }

    public static <T extends CharSequence> void notContain(CharSequence textToSearch, T substring, String code, String message) {
        isTrue(!StrUtil.contains(textToSearch, substring), code, message);
    }

    public static <T> void isContain(Collection<T> collection, T item, String code, String message) {
        isTrue(CollUtil.contains(collection, item), code, message);
    }

    public static <T> void notContain(Collection<T> collection, T item, String code, String message) {
        isTrue(!CollUtil.contains(collection, item), code, message);
    }

    public static void isEquals(Object obj1, Object obj2, String code, String message) {
        isTrue(ObjectUtil.equal(obj1, obj2), code, message);
    }

    public static void notEquals(Object obj1, Object obj2, String code, String message) {
        isTrue(!ObjectUtil.equal(obj1, obj2), code, message);
    }

}
